package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.Disease;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/disease")
public class DiseaseController {
    @Autowired
    private Disease disease;
    @RequestMapping(value = "/insertDisease",method = RequestMethod.POST)
    public String insertDisease(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        String diseasecode = (String)para.get("diseasecode");
        String diseasename = (String)para.get("diseasename");
        String diseaseicd =(String) para.get("diseaseicd");
        Integer disecategoryid =Integer.parseInt((String) para.get("disecategoryid"));
        Integer DelMark =Integer.parseInt((String) para.get("delmark"));
        com.foreknow.entity.Disease disease1 = new com.foreknow.entity.Disease();
        disease1.setDiseasecode(diseasecode);
        disease1.setDiseasename(diseasename);
        disease1.setDiseaseicd(diseaseicd);
        disease1.setDisecategoryid(disecategoryid);
        disease1.setDelmark(DelMark);
        int i = disease.insertDisease(disease1);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("添加疾病成功",200));
        }else {
            hs.put("meta",new Constance("添加疾病失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteDiseaseById",method = RequestMethod.GET)
    public String deleteDiseaseById(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        Integer id =Integer.parseInt((String) para.get("id"));
        HashMap<String,Object> hs=new HashMap<>();
        int i = disease.deleteById(id);
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除疾病成功",200));
        }else {
            hs.put("meta",new Constance("删除疾病失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteDiseaseByIds",method = RequestMethod.GET)
    public String deleteDiseaseByIds(@RequestParam Map<String,List<Integer>> ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int i = disease.delectByids(ids.get("ids"));
        if(i>0){
            hs.put("meta", new Constance("删除成功！"+i+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/updateDiseaseById",method = RequestMethod.GET)
    public String updateDiseaseById(@RequestBody com.foreknow.entity.Disease disease1)throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int i = disease.updateById(disease1);
        if (i>0){
            hs.put("meta", new Constance("修改成功", 200));
        }else {
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/findDisease",method = RequestMethod.GET)
    public String findDisease(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        String param = (String)para.get("query");
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize=Integer.parseInt((String) para.get("pageSize"));
        HashMap<String,Object> hs=new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<com.foreknow.entity.Disease> diseases = disease.findUsers(param);
        PageInfo<com.foreknow.entity.Disease> pageInfo = new PageInfo<>(diseases);
        //给map添加数据
        hs.put("totalpage",pageInfo.getTotal());
        hs.put("pagenum",pageInfo.getPageNum());
        hs.put("disease",diseases);
        if (diseases.size()>0){
            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("获取失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/searchDisea",method = RequestMethod.POST)
    public String searchDisea(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        ObjectMapper objectMapper = new ObjectMapper();
        Object heightQuery = para.get("heightQuery");
        com.foreknow.entity.Disease disease1 = objectMapper.convertValue(heightQuery, com.foreknow.entity.Disease.class);
        Integer pageNum = Integer.parseInt(String.valueOf(para.get("pageNum")));
        Integer pageSize = Integer.parseInt(String.valueOf(para.get("pageSize")));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<com.foreknow.entity.Disease> diseaseList = disease.hightSerchDisease(disease1);
        PageInfo<com.foreknow.entity.Disease> info = new PageInfo<>(diseaseList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("disease", diseaseList);
        if (diseaseList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取到"+info.getTotal()+"条数据", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/findSDiseaseById",method = RequestMethod.GET)
    public String findSDiseaseById(@RequestParam Integer id) throws JsonProcessingException {
        com.foreknow.entity.Disease id1 = disease.findById(id);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",id1);
        if (id1!=null){
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
